package com.wts.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;

import com.wts.client.https.HttpUtils;
import com.wts.client.utils.WtcJsonMap;

/**
 * 业务相关
 * 
 * @author macpl
 *
 */
public class WtsExamClient {
	private String secret;
	private String operatorLoginname;
	private String operatorPassword;
	private String baseUrl;

	public WtsExamClient(String baseUrl, String secret, String operatorLoginname, String operatorPassword) {
		super();
		this.secret = secret;
		this.baseUrl = baseUrl;
		this.operatorLoginname = operatorLoginname;
		this.operatorPassword = operatorPassword;
	}

	/**
	 * 服务是否可用
	 * 
	 * @return
	 */
	public boolean isLive(boolean throwExceptionAble) {
		Map<String, String> data = new HashMap<String, String>();
		data.put("secret", secret);
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		// -----------
		data.put("loginname", operatorLoginname);
		JSONObject backData = HttpUtils.httpPost(baseUrl + "/api/auth/get/user.do", data);
		if (!backData.has("STATE") || backData.getInt("STATE") != 0) {
			if (throwExceptionAble) {
				throw new RuntimeException(backData.toString());
			} else {
				return false;
			}
		}
		return true;
	}

	/**
	 * 获得答题室列表
	 * 
	 * @param states     状态 :00暂停，10新建，11重置，20发布，21开始答题，22结束答题，30结束阅卷
	 * @param pshowtypes 答卷展示类型:1 :标准答題,2:抽卷答題,3:练习,4:学习5.问卷6.成绩补录
	 * @return
	 */
	public List<WtcJsonMap> getRooms(List<String> states, List<String> pshowtypes) {
		Map<String, String> data = new HashMap<String, String>();
		data.put("secret", secret);
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		// -----------
		if (states != null)
			data.put("states", StringUtils.join(states, ","));
		if (pshowtypes != null)
			data.put("pshowtype", StringUtils.join(pshowtypes, ","));
		JSONObject backData = HttpUtils.httpPost(baseUrl + "/api/room/list/info.do", data);
		if (!backData.has("STATE") || backData.getInt("STATE") != 0) {
			throw new RuntimeException(backData.toString());
		}
		WtcJsonMap json = new WtcJsonMap(backData.toMap());
		return json.getList("DATA");
	}

	/**
	 * 获得答题室内
	 * 
	 * @param roomid
	 * @param pshowtypes
	 * @return
	 */
	public WtcJsonMap getRoom(String roomid, boolean isUUID) {
		Map<String, String> data = new HashMap<String, String>();
		data.put("secret", secret);
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		// -----------
		if (isUUID) {
			data.put("roomuuid", roomid);
		} else {
			data.put("roomid", roomid);
		}
		JSONObject backData = HttpUtils.httpPost(baseUrl + "/api/room/info.do", data);
		if (!backData.has("STATE") || backData.getInt("STATE") != 0) {
			throw new RuntimeException(backData.toString());
		}
		WtcJsonMap json = new WtcJsonMap(backData.toMap());
		return json;
	}

	/**
	 * 获得答题卡
	 * 
	 * @param roomuuid    答题室UUID 选填,答题室UUID
	 * @param paperuuid   答卷UUID 选填,答卷UUID
	 * @param loginname   用户登录名 选填,用户登录名
	 * @param backversion 归档时间:必填,LIVE为当前答题室，HIS归档答题室数据或者14位日期时间组合的归档时间
	 * @return
	 */
	public WtcJsonMap getCards(String roomuuid, String paperuuid, String loginname, String backversion) {
		Map<String, String> data = new HashMap<String, String>();
		data.put("secret", secret);
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		// -----------
		if (StringUtils.isNotBlank(roomuuid)) {
			data.put("roomuuid", roomuuid);
		}
		if (StringUtils.isNotBlank(paperuuid)) {
			data.put("paperuuid", paperuuid);
		}
		if (StringUtils.isNotBlank(loginname)) {
			data.put("loginname", loginname);
		}
		if (StringUtils.isNotBlank(backversion)) {
			data.put("backversion", backversion);
		}
		JSONObject backData = HttpUtils.httpPost(baseUrl + "/api/card/list.do", data);
		if (!backData.has("STATE") || backData.getInt("STATE") != 0) {
			throw new RuntimeException(backData.toString());
		}
		WtcJsonMap json = new WtcJsonMap(backData.toMap());
		return json;
	}
}
